import { AppCTX } from "../../types/koaExtend";
import { createBrandApiMWRouter } from "../../middleware/brand-api/brandAdminChecker";
import pageMW from "../../middleware/pager";
const router = createBrandApiMWRouter();
import {
  aggrActivityStatsOfBrandByActivityID,
  aggrConsignmentByActivityID,
  aggrConsignmentForSpokesmanByActivityID,
  rankActivityItemOfStoreForDistributorWithStatsOverOrderAmount,
  rankOverOrderAmountForSpokesmanByActivityID,
} from "../../service/activityStatService";

// 获取活动帮卖统计
router.get(
  "/brd/:brandId/activity/:activityId/statistics/consignment",
  async (ctx: AppCTX, next) => {
    const { brandId, activityId } = ctx.params;
    const { userId } = ctx;

    const activityConsignmentAggrInfo = await aggrConsignmentByActivityID(
      activityId
    );
    const spokesmanActivityConsignmentAggrInfos =
      await aggrConsignmentForSpokesmanByActivityID(activityId);
    ctx.body = {
      activityConsignmentAggrInfo,
      spokesmanActivityConsignmentAggrInfos,
    };
  }
);

// 获取活动收入统计
router.get(
  "/brd/:brandId/activity/:activityId/statistics/income",
  async (ctx: AppCTX, next) => {
    const { brandId, activityId } = ctx.params;
    const { userId } = ctx;

    const aggrRet = await aggrActivityStatsOfBrandByActivityID(
      activityId,
      brandId
    );
    ctx.body = aggrRet;
  }
);

// 获取分销员门店活动排名
router.get(
  "/brd/:brandId/activitiesRankByDistributor",
  pageMW,
  async (ctx: AppCTX, next) => {
    const { brandId } = ctx.params;
    const { distributorId } = ctx.query;
    const { userId, limit, offset, ts } = ctx;

    const { total, items } =
      await rankActivityItemOfStoreForDistributorWithStatsOverOrderAmount(
        distributorId,
        {
          limit,
          offset,
          ts,
        }
      );
    ctx.body = {
      total,
      rows: items,
    };
  }
);

// 获取活动代言人排名
router.get(
  "/brd/:brandId/activity/:activityId/statistics/spokesmanRank",
  async (ctx: AppCTX, next) => {
    const { brandId, activityId } = ctx.params;
    const { topRankNumber } = ctx.query;
    const { userId } = ctx;

    const rankOverRet = await rankOverOrderAmountForSpokesmanByActivityID(
      activityId,
      topRankNumber ?? 5
    );
    ctx.body = rankOverRet;
  }
);

export default router;
